#include "ArmoredVehicle.h"
#include "RSManager.h"
#include "Sprite.h"
#include "Box.h"
#include "Player.h"
#include "ShareComponents.h"
#include "BulletManager.h"
#include "Camera.h"
ArmoredVehicle::ArmoredVehicle(Box* box) : Object(box, ObjAnti_DYNAMID), MoveObject()
{
	_arrSpriteB = new Sprite*[3];
	_arrSpriteY = new Sprite*[3];
	_arrSpriteP = new Sprite*[3];
	_arrSpriteR = new Sprite*[3];

	_arrSpriteB[0] = RSManager::getInstance()->getSprite(ARMORED_B1);
	_arrSpriteB[1] = RSManager::getInstance()->getSprite(ARMORED_B2);
	_arrSpriteB[2] = RSManager::getInstance()->getSprite(ARMORED_B3);

	_arrSpriteY[0] = RSManager::getInstance()->getSprite(ARMORED_Y1);
	_arrSpriteY[1] = RSManager::getInstance()->getSprite(ARMORED_Y2);
	_arrSpriteY[2] = RSManager::getInstance()->getSprite(ARMORED_Y3);

	_arrSpriteP[0] = RSManager::getInstance()->getSprite(ARMORED_P1);
	_arrSpriteP[1] = RSManager::getInstance()->getSprite(ARMORED_P2);
	_arrSpriteP[2] = RSManager::getInstance()->getSprite(ARMORED_P3);

	_arrSpriteR[0] = RSManager::getInstance()->getSprite(ARMORED_R1);
	_arrSpriteR[1] = RSManager::getInstance()->getSprite(ARMORED_R2);
	_arrSpriteR[2] = RSManager::getInstance()->getSprite(ARMORED_R3);

	_hits = 100;
	_isAlive = true;
	_isMoving = true;
	_sprite = _arrSpriteB[0];
	_colBox->_position = _box->_position;
	_colBox->_size = _box->_size;
	_colBox->_v = _box->_v;
	_BulletPos = D3DXVECTOR2(40, 100);
	delay = 0;
	gundelay =0;
	_bcount=  3;	
	_state = BLACK;
	_index = 0;
	_timeMoving = 0;
	AssignMove(_box);
}


ArmoredVehicle::~ArmoredVehicle(void)
{
}

void ArmoredVehicle::update()
{
	delay += GAME_TIME;	
	_timeMoving += GAME_TIME;
	if (_timeMoving >= 1800)	
		_isMoving = false;
	if(_timeMoving >= 15000)
		_isMoving=true;

	if(_isMoving)
	{
		if (delay >= 150)
		{
			delay = 0;
			_box->_v.x = -2;
			_sprite->Next();
		}
		move();	
	}
	else
	{
		if(_bcount == 3)
			getIndex();
		if(_hits >= 75)
			_state = BLACK;
		else
		{
			if(_hits >= 50)
				_state = YELLOW;
			else
			{
				if(_hits >= 25)
					_state = PINK;
				else
					if(_hits > 0)
						_state = RED;
					else
						_isAlive = false;
			}
		}

		switch (_state)
		{
		case BLACK:
			_sprite = _arrSpriteB[_index];
			break;
		case YELLOW:
			_sprite = _arrSpriteY[_index];
			break;
		case PINK:
			_sprite = _arrSpriteP[_index];
			break;
		case RED:
			_sprite = _arrSpriteR[_index];
			break;
		default:
			break;
		}

		gundelay+=GAME_TIME;
		if(delay >1500)
		{
			if(gundelay >200)
			{
				gundelay =0;
				_bcount--;
				BulletManager::getInstance()->shoot(B_E,this->_obj,_box->_position + _BulletPos,(_index+8)*3.14/8 );							
			}
			if(_bcount==0)
			{
				delay =0;
				_bcount=3;
			}
		}
	}		
}

void ArmoredVehicle::render()
{
	_sprite->Render(_box->_position);
}

Box* ArmoredVehicle::getCollisionBox()
{
	return _colBox;
}

void ArmoredVehicle::getIndex()
{
	D3DXVECTOR2 dis = (_box->_position + _BulletPos) - gl_player->_position;
	float angle = atan2(dis.y, dis.x);	
	angle *= 180/3.14f;
	if(angle > 45)
		_index =2;
	else 
		if(angle > 15)
			_index =1;
		else			
			_index = 0;
}
